package neu.soft.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import neu.soft.constant.OrderStatus;

import neu.soft.entity.DO.ReceiptJournal;
import neu.soft.entity.NormalOrder;
import neu.soft.entity.User;
import neu.soft.service.OrderService;
import neu.soft.service.SenderService;
import neu.soft.vo.MsgBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Api(value = "订单管理模块的API",description="客服对订单的维护和生成以及与其他部门的数据交互")

public class  OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    SenderService senderService;

    String userName="123";
    @PostMapping("/all")
    @ApiOperation(value = "查询所有的订单信息 无参" )
    public ResultBean findAllOrder(@RequestBody Map<String, Object> map){
        Integer pageNum;
        Integer pageSize;
        pageNum = (Integer) map.get("pageNum");
        pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<NormalOrder> allOrder = orderService.findAllOrder();
        PageInfo<NormalOrder> page = new PageInfo<>(allOrder, 5);
        if(allOrder.size()==0){
            return ResultBean.error();
        }
       // senderService.sendMoney(new MsgBean<>(1,"hahaha"));
        return new ResultBean(200,"FIND SUCCESS",page);
    }


    @PostMapping("/findById")
    @ApiOperation(value = "根据订单id查找订单，" + "json  参数  String oid" )
    public ResultBean findOrderById(@RequestBody Map<String, Object> map){

        if (orderService.findById(map)!=null){
            NormalOrder normalOrder = orderService.findById(map);
            return ResultBean.success("SHOW SUCCESS",normalOrder);
        }
        return ResultBean.error();
    }


    /**
 * @param normalOrder 要求商品list必须有商品且商品信息一定是完善的，商品代码、价格、数量、名称缺一不可
 * */
    @PostMapping("/add")
    @ApiOperation(value = "添加订单，需要 订单对象，要求订单中的商品list必须有商品，且商品信息一定是完善的，商品代码、价格、数量、名称缺一不可" )
    public ResultBean addOrder(@RequestBody NormalOrder normalOrder) throws InterruptedException {
        if (orderService.addOrder(normalOrder)!=null){
            //配送单
            boolean sendMsg = senderService.sendMsg(new MsgBean(normalOrder, "order -- " + normalOrder.getOid()));
            if(sendMsg){
                System.out.println("--------------发送普通订单！-----------------");
            }
            ReceiptJournal receiptJournal = new ReceiptJournal();
            receiptJournal.setOid(normalOrder.getOid());
            receiptJournal.setTotalPay(normalOrder.getTotalPay());
            receiptJournal.setUserName(userName);//username 123
            //发钱
          //  Thread.sleep(1000);
            boolean money = senderService.sendMoney(new MsgBean(receiptJournal, "MONEY"));
            if(money){
                System.out.println("--------------发送订单的金额！---------------");
            }
            return ResultBean.success("ADD SUCCESS",normalOrder);
        }
        return ResultBean.error();
    }

    /**
     * @param map 参数是String类型  oid  和  status ，根据id查到后更改订单状态为status
     * */
    @PostMapping("/change")
    @ApiOperation(value = "修改订单状态，" + "参数  String oid和 String status" )
    public ResultBean changeOrderStatus(@RequestBody Map<String, Object> map){
        OrderStatus status = OrderStatus.getItem(map.get("status").toString());
        map.replace("status", status);
        if(status==OrderStatus.Finished_Distribution){
            boolean msg = senderService.sendMsgForExchange(new MsgBean<>(map.get("oid"), "订单已完成！"));
            if(msg){
                System.out.println("------------发送已完成订单消息！----------------");
            }
        }
        if (orderService.changeOrderStatus(map)){
            return ResultBean.success("ADD SUCCESS");
        }
        return ResultBean.error();
    }

    /**
     * @param map 参数是String类型  oid ，根据id查到后返回订单状态
     * */
    @PostMapping("/showStatus")
    @ApiOperation(value = "查看订单状态，" + "参数  String oid" )
    public ResultBean showStatus(@RequestBody Map<String, Object> map){

        if (orderService.showStatus(map)!=null){
            OrderStatus orderStatus =orderService.showStatus(map);
            return ResultBean.success("SHOW SUCCESS", orderStatus);
        }
        return ResultBean.error();
    }

    /**
     * @param map 参数是String类型  oid ，根据id查到后将订单状态改为
     * */
    @PostMapping("/returnSales")
    @ApiOperation(value = "生成退货单，" + "参数  String oid" )
    public ResultBean returnSales(@RequestBody Map<String, Object> map){

            NormalOrder normalOrder = orderService.returnSales(map);
            if(normalOrder!=null){
                boolean returnSlaes = senderService.sendMsg(new MsgBean<>(normalOrder, "ReturnSlaes"));
                if(returnSlaes){
                    System.out.println("------------发送退货单！----------------");
                }
                return ResultBean.success("RETURN SUCCESS",normalOrder);
            }
        return ResultBean.error();
    }
/**
 * @param normalOrder 参数  完整的原订单，包含原订单商品和新订单商品
 * @return 返回一个新的订单
 * */
    @PostMapping("/exchange")
    @ApiOperation(value = "生成换货单，即新订单加原订单状态更改" + "参数  完整的原订单，包含原订单商品和新订单商品  返回一个新的订单" )
    public ResultBean exchangeOrder(@RequestBody NormalOrder normalOrder){

        NormalOrder newOrder = orderService.exchangeSales(normalOrder);
        if(newOrder!=null){
            boolean exchange = senderService.sendMsg(new MsgBean<>(normalOrder, "Exchange"));
            if(exchange){
                System.out.println("-----------------发送换货单！-------------------");
            }

            return ResultBean.success("EXCHANGE SUCCESS",newOrder);
        }
        return ResultBean.error();
    }
    @PostMapping("/lack")
    @ApiOperation(value = "生成缺货单" + "参数  订单类型，需要type为缺货且有shoppinglist" )
    public ResultBean lackOrder(@RequestBody NormalOrder normalOrder){
        orderService.addOrder(normalOrder);
        Map<String,Object>map = orderService.lackSales(normalOrder);

        if(map!=null){
            boolean lack = senderService.sendMsgForLack(new MsgBean(map, "lackSale  --  " + normalOrder.getOid()));
            if(lack){
                System.out.println("-----------发送缺货单！-----------");
            }
            return ResultBean.success("EXCHANGE SUCCESS",map);
        }
        return ResultBean.error();
    }


    /**
     * @param map 参数  需要取消的订单
     * @return 是否成功取消
     * */
    @PostMapping("/cancel")
    @ApiOperation(value = "取消订单，判断订单状态是否允许取消并执行取消" + "参数  需要取消订单的oid " )
    public ResultBean cancelOrder(@RequestBody Map<String, Object> map){
        if(orderService.cancelOrder(map)){
            return ResultBean.success("CANCEL SUCCESS");
        }
        return ResultBean.error();
    }

    @PostMapping("/pro")
    @ApiOperation(value = "123456 " )
    public ResultBean produce(@RequestBody MsgBean msgBean){

            return ResultBean.success("CANCEL SUCCESS",senderService.sendMsgForLack(msgBean));
    }

    @PostMapping("/userOrder")
    @ApiOperation(value = "用户id获取所有订单" + "参数  uid " )
    public ResultBean userOrder(@RequestBody Map<String, Object> map){
        Integer pageNum;
        Integer pageSize;
        pageNum = (Integer) map.get("pageNum");
        pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<NormalOrder> orders = orderService.findUserOrder(map);
        PageInfo<NormalOrder> page = new PageInfo<>(orders, 5);

            if(orders!=null){
                return ResultBean.success("Find SUCCESS",page);
        }
        return ResultBean.error();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除无效订单" + "参数  oid " )
    public ResultBean deleteUselessOrder(@RequestBody Map<String, Object> map){
        int deleteUselessOrder = orderService.deleteUselessOrder(map);
        if(deleteUselessOrder!=0){
            return ResultBean.success("Delete SUCCESS");
        }
        return ResultBean.error();
    }


}
